package com.war.service.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.war.dao.system.BuyerDao;
import com.war.pojo.system.Buyer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class BuyerServiceImpl implements BuyerService{
    @Autowired(required = false)
    BuyerDao buyerDao;

    @Override
    public HashMap<String, Object> selectAll() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Buyer> list = buyerDao.selectAll();
        map.put("list",list);
        return map;
    }

    @Override
    public List<Buyer> selectName() {
        return buyerDao.selectName();
    }

    @Override
    public HashMap<String, Object> select(Buyer buyer) {
        HashMap<String, Object> map =  new HashMap<String, Object>();
        //1 设置分页参数
        PageHelper.startPage(buyer.getPage(),buyer.getRow());
        //根据用户选中的查询条件判断用户需要查询的
        List<Buyer> list = new ArrayList<>();
        if(buyer.getConValue().equals("")){
            list = buyerDao.selectAll();
        }else {
            if(buyer.getCondition().equals("姓名")){
                //设置查询条件
                buyer.setName(buyer.getConValue());
                list.add( buyerDao.selectByName(buyer));
            }else {
                list = buyerDao.selectAll();
            }
        }
        //3.把查询的数据转换成分页对象
        PageInfo<Buyer> page = new PageInfo<Buyer>(list);

        //获取分页的当前页的集合
        map.put("list",page.getList());
        //总条数
        map.put("total",page.getTotal());
        //总页数
        map.put("totalPage",page.getPages());
        //上一页
        if(page.getPrePage()==0){
            map.put("pre",1);
        }else{
            map.put("pre",page.getPrePage());
        }

        //下一页
        //保持在最后一页
        if(page.getNextPage()==0){
            map.put("next",page.getPages());
        }else{
            map.put("next",page.getNextPage());
        }
        //当前页
        map.put("cur",page.getPageNum());

        return map;
    }

    @Override
    public Buyer selectByid(Buyer buyer) {
        return buyerDao.selectById(buyer);
    }

    @Override
    public String delBuy(Buyer buyer){
        int num = buyerDao.delWare(buyer);
        if(num>0){
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    public String addBuy(Buyer buyer){
        Buyer w = buyerDao.selectByName(buyer);
        if(w != null && w.getId() != buyer.getId() ){
            return "当前仓库名已被注册";
        }else {
            int n = buyerDao.addWare(buyer);
            if(n>0){
                return "添加成功";
            }
            return "添加失败";
        }
    }

    @Override
    public String editBuy(Buyer buyer) {
        Buyer w = buyerDao.selectByName(buyer);
        if(w != null && w.getId() != buyer.getId() ){
            return "当前仓库名已被注册";
        }else {
            int n = buyerDao.updateWare(buyer);
            if(n>0){
                return "修改成功";
            }
            return "修改失败";
        }
    }

}
